﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DAL;
using DevExpress.Web.ASPxEditors;

public partial class ConsumerManage_RefCompanyEdit : Frame.Pages.BaseInfomationEditBasePage, Frame.Pages.IDetailEditManPage<BI_Consumer>
{
    protected override bool IsAddCommandColumn
    {
        get
        {
            return false;
        }
    }

    /// <summary>
    /// 权限编码，需要重新设置
    /// </summary>
    protected override string ActionID
    {
        get { return "0"; }
    }


    public int ComID
    {
        get
        {
            int i=0;
            if (int.TryParse(Request.QueryString["comid"], out i))
            {
                return i;
            }
            else
            {
                return -1;
            }
        }
    }

    protected string GetCompanyName(object CompanyID)
    {
        if (Cache["CompanyList"] == null)
        {
            using (DAL.CMSDALDataContext context = new DAL.CMSDALDataContext())
            {
                Cache["CompanyList"] = context.BI_Company.ToList();
            }
        }
        List<BI_Company> list = Cache["CompanyList"] as List<BI_Company>;
        BI_Company model = list.FirstOrDefault(o => o.CompanyID == (int)CompanyID);
        if (model != null)
        {
            return model.CompanyName;
        }
        return string.Empty;
    }
    protected void gridView_HtmlRowPrepared(object sender, DevExpress.Web.ASPxGridView.ASPxGridViewTableRowEventArgs e)
    {
        if (e.RowType == DevExpress.Web.ASPxGridView.GridViewRowType.Data)
        {
            if (e.GetValue("RefrenceCompany_ID") != null)
            {
                e.Row.BackColor = System.Drawing.Color.Pink;
            }
        }
    }
    protected void LSMDSourceMain_Selecting(object sender, DevExpress.Data.Linq.LinqServerModeDataSourceSelectEventArgs e)
    {
        DAL.CMSDALDataContext context = new CMSDALDataContext();
        int comid = ComID;
        if (comid==-1)
        {
            this.Response.Redirect("~/Default.aspx");
        }
        e.QueryableSource = context.BI_Consumer.Where(o => o.RefrenceCompany_ID == comid);
    }
    protected void gridView_CustomCallback(object sender, DevExpress.Web.ASPxGridView.ASPxGridViewCustomCallbackEventArgs e)
    {
        List<string> IDlist = gridView.GetSelectedFieldValues("ConsumerID").Select<object, string>(o => o.ToString()).ToList();
        using (CMSDALDataContext context = new CMSDALDataContext())
        {
            foreach (string item in IDlist)
            {

                BI_Consumer model = context.BI_Consumer.FirstOrDefault(o => o.ConsumerID == long.Parse(item));
                context.BI_Consumer.DeleteOnSubmit(model);
            }
            context.SubmitChanges();
        }
        this.gridView.DataBind();
    }
    #region IDetailEditManPage<BI_Consumer> 成员

    public string BackUrl
    {
        get;
        set;
    }

    public BI_Consumer Model
    {
        get;
        set;
    }

    public string ScriptBackUrl
    {
        get;
        set;
    }

    #endregion

    protected void btn_Click(object sender, EventArgs e)
    {
        ASPxButton btn = sender as ASPxButton;
        if (btn == null)
        {
            return;
        }
        else
        {
            BackUrl = string.Format("~/ConsumerManage/RefCompanyEdit.aspx?comid={0}",ComID);
            ScriptBackUrl = string.Format("../ConsumerManage/EditConsumer.aspx?comid={0}", ComID);
            using (CMSDALDataContext dcontext = new CMSDALDataContext())
            {
                Model = dcontext.BI_Consumer.FirstOrDefault(o => o.ConsumerID == int.Parse(btn.CommandArgument));
                if (Model == null)
                {
                    Model = new BI_Consumer();
                }
            }
            switch (btn.CommandName.ToLower())
            {
                case "book":
                    {
                        Server.Transfer("~/BLIInfo/EditBook.aspx");
                        break;
                    }
                case "consumer":
                    {
                        break;
                    }
                case "returnvisit":
                    {
                        break;
                    }
                case "mday":
                    {
                        Server.Transfer("~/BLIInfo/EditConsumerMDay.aspx");
                        break;
                    }
                case "sms":
                    {
                        Server.Transfer("~/BLIInfo/EditSMSSendDetail.aspx");
                        break;
                    }
                case "update":
                    {
                        Session["model"] = Frame.Pages.RefrenceContext<DAL.BI_Consumer>.Copy(this);
                        Response.Redirect("~/ConsumerManage/EditDetailConsumer.aspx");
                        break;
                    }
                default:
                    break;
            }
        }
    }
    protected void BtnNew_Click(object sender, EventArgs e)
    {
        Response.Redirect(string.Format("~/ConsumerManage/ConsumerSearchEdit.aspx?comid={0}", ComID));
    }
}
